package com.fch.sdlibrary.service.impl;

import com.fch.sdlibrary.entity.BookClazz;
import com.fch.sdlibrary.mapper.BookClazzMapper;
import com.fch.sdlibrary.service.BookClazzService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @program: sdlibrarySpring
 * @description: 图书分类接口实现类
 * @CreatedBy: fch
 * @create: 2022-05-07 19:02
 **/
@Service
public class BookClazzServiceImpl implements BookClazzService {
    @Resource
    private BookClazzMapper bookClazzMapper;

    @Override
    public List<BookClazz> queryL1() {
        List<BookClazz> bookClazzes = new ArrayList<>();
        bookClazzes = bookClazzMapper.queryL1();
        return bookClazzes;
    }

    @Override
    public List<BookClazz> queryL2(String bookCode) {
        List<BookClazz> bookClazzes = new ArrayList<>();
        bookClazzes = bookClazzMapper.queryL2(bookCode);
        return bookClazzes;
    }

    @Override
    public List<BookClazz> queryL3(String bookCode) {
        List<BookClazz> bookClazzes = new ArrayList<>();
        bookClazzes = bookClazzMapper.queryL3(bookCode);
        return bookClazzes;
    }

    /**
     * @Author: fch
     * @Description: 获取图书完整分类名
     * @DateTime: 13:55 8/5/2022
     * @Params: [bookId]
     * @Return: java.lang.String
     **/
    @Override
    public String getBookClassName(String code) {
        StringBuilder sb = new StringBuilder();
        BookClazz bookClazz = new BookClazz();
        bookClazz.setClassCode(code);
        bookClazz = bookClazzMapper.queryParent(bookClazz);
        String n = bookClazzMapper.queryParent(bookClazz).getClassificationName();
        while (bookClazz.getClassLayer() != 1) {
            bookClazz.setClassCode(bookClazz.getClassParent());
            bookClazz = bookClazzMapper.queryParent(bookClazz);
            try {
                sb.insert(0, bookClazz.getClassificationName() + "/");
            } catch (Exception e) {
                e.printStackTrace();
                return n;
            }
        }
        return sb.toString() + n;
    }

    @Override
    public boolean isExist(String bookCode) {
        return bookClazzMapper.queryBookClazzByCode(bookCode) != null;
    }

    @Override
    public boolean AddClazzL1(BookClazz bookClazz) {
        String maxCode = bookClazzMapper.queryMaxCodeL1() ;
        if (Integer.parseInt(maxCode) + 1 < 10)
            bookClazz.setClassCode("0" + (Integer.parseInt(maxCode) + 1));
        else
            bookClazz.setClassCode("" + (Integer.parseInt(maxCode) + 1));
        return bookClazzMapper.addBookClass(bookClazz) > 0;
    }

    @Override
    public boolean AddClazzChild(BookClazz bookClazz) {
        String maxCode = bookClazzMapper.queryMaxCodeL2(bookClazz);
        if (maxCode == null){
            bookClazz.setClassCode(bookClazz.getClassParent() + "01");
        }
        else{
            maxCode = maxCode.substring(bookClazz.getClassParent().length());
            if (Integer.parseInt(maxCode) + 1 < 10)
                bookClazz.setClassCode(bookClazz.getClassParent() + "0" + (Integer.parseInt(maxCode) + 1));
            else
                bookClazz.setClassCode(bookClazz.getClassParent() + (Integer.parseInt(maxCode) + 1));
        }
        return bookClazzMapper.addBookClass(bookClazz) > 0;
    }

    @Override
    public List<BookClazz> getBookClazzList(String bookClassCode) {
        List<BookClazz> list = new ArrayList<>();
        BookClazz bookClazz = bookClazzMapper.queryBookClazzByCode(bookClassCode);
        list.add(bookClazz);
        while (bookClazz.getClassLayer() != 1) {
            bookClazz = bookClazzMapper.queryBookClazzByCode(bookClazz.getClassParent());
            list.add(bookClazz);
        }
        list.sort(Comparator.comparingInt(BookClazz::getClassLayer));
        return list;
    }

//    @Override
//    public boolean AddClazzL3(BookClazz bookClazz) {
//        String maxCode = bookClazzMapper.queryMaxCodeL3(bookClazz);
//        if (maxCode == null){
//            bookClazz.setClassCode(bookClazz.getClassParent() + "01");
//        }
//        else{
//            maxCode = maxCode.substring(bookClazz.getClassParent().length());
//            if (Integer.parseInt(maxCode) + 1 < 10)
//                bookClazz.setClassCode(bookClazz.getClassParent() + "0" + (Integer.parseInt(maxCode) + 1));
//            else
//                bookClazz.setClassCode(bookClazz.getClassParent() + (Integer.parseInt(maxCode) + 1));
//        }
//        return bookClazzMapper.addBookClass(bookClazz) > 0;
//    }


}
